<?php
	namespace Api\Controller;
	use Think\Controller;
	header("Content-Type:text/html;charset=utf-8");
	class AnnunciateController extends Controller
	{
		/* 
		 *通告详情 
		 *  
		 */
		public function annunciateDetails($id=null, $uid=null)
		{
			$where["id"] = $id;
			$annunciateInfo = M("Annunciate") -> where($where) -> find();
// 			var_dump($annunciateInfo);
			if ($annunciateInfo != null) 
			{
				
			
				//通告基本信息
				if ($annunciateInfo["basic"] == null) 
				{
					$annunciateInfo["basic"] = "";
				}
				
				//通告详情
				if ($annunciateInfo["annunciate_w"] == null)
				{
					$annunciateInfo["annunciate_w"] = "";
				}
				//该通告已报名人数及个人信息
				if ($annunciateInfo["applypeople"] == null) 
				{
					$applyedPeople = array();
					//已报名人数
					$Number = 0;
					$applyedType = 0;//用户未报名
				}
				else
				{
					//已报名的人数的uid
					$applyedUids = explode(",", $annunciateInfo["applypeople"]);
					$condition["uid"] = array("in", $applyedUids);
					//判断登录的用户是否已报名
					if ($uid == null) 
					{
						$applyedType = 0;//用户未报名
					}
					else
					{
						if (in_array($uid, $applyedUids)) 
						{
							$applyedType = 1;//用户已报名
						}
						else
						{
							$applyedType = 0;//用户未报名
						}
					}
					//报名总人数
					$Number = count($applyedUids); 
					$peopleInfo = M("User") -> field("uid,type,rmtype,dtttype") -> where($condition) -> order("dtttype desc, rmtype desc, regtime desc") -> select();
					if ($peopleInfo == null) 
					{
						$applyedPeople = array();
					}
					else
					{
						foreach ($peopleInfo as $key=>$value)
						{
							if ($value["type"] == 0) 
							{
								//个人
								if ($value["rmtype"] == 1) 
								{
									//个人已完善资料
									$uid1[] = $value["uid"];
								}
								else
								{
									//个人未完善资料
									$uid2[] = $value["uid"];
								}
							}
							else
							{
								//企业
								if ($value["rmtype"] == 1)
								{
									//企业已完善资料
									$uid3[] = $value["uid"];
								}
								else
								{
									//企业未完善资料
									$uid4[] = $value["uid"];
								}
							}
							
						}
						if ($uid1 == null) 
						{
							$userInfo1 = null;
						}
						else
						{
							$condition1["think_userinfo.uid"] = array("in", $uid1);
							$userInfo1 = M("Userinfo") -> join("__USER__ on __USER__.uid=__USERINFO__.uid") -> field("think_userinfo.uid,type,dtttype,headimg") -> where($condition1) -> select();
							if ($userInfo1 == null) 
							{
								$userInfo1 = null;
							}
							else
							{
								foreach ($userInfo1 as $key1=>$value1)
								{
									if ($value1["uid"] == null) 
									{
										$value1["uid"] = "";
									}
									if ($value1["headimg"] == null) 
									{
										$value1["headimg"] = "";
									}
								}
							}
						}
						//第二种情况
						if ($uid2 == null) 
						{
							$userInfo2 = null;
						}
						else
						{
							$condition2["uid"] = array("in", $uid2);
							$userInfo2 = M("User") -> field("uid,type,dtttype") -> where($condition2) -> select();
							if ($userInfo2 == null) 
							{
								$userInfo2 = null;
							}
							else
							{
								foreach ($userInfo2 as $key2=>$value2)
								{
									$value2["headimg"] = "";
									$userInfo2[$key2] = $value2;
								}
							}
							
						}
						//第三种情况 已报名的为企业用户并且企业用户已完善资料
						if ($uid3 == null) 
						{
							$userInfo3 = null;
						}
						else
						{
							$condition3["think_company.uid"] = array("in", $uid3);
							$userInfo3 = M("Company") -> join("__USER__ on __USER__.uid=__COMPANY__.uid") -> field("think_company.uid,type,dtttype,cplogo") -> where($condition3) -> select();
							if ($userInfo3 == null) 
							{
								$userInfo3 = null;
							}
							else
							{
								foreach ($userInfo3 as $key3=>$value3)
								{
									if ($value3["cplogo"] == null) 
									{
										unset($value3["cplogo"]);
										$value3["headimg"] = "";
									}
									else
									{
										$headimg = $value3["cplogo"];
										unset($value3["cplogo"]);
										$value3["headimg"] = $headimg;
									}
									$userInfo3[$key3] = $value3;
								}
							}
						}
						//第四种情况  报名的为企业用户并且未完善过资料
						if ($uid4 == null) 
						{
							$userInfo4 = null;
						}
						else
						{
							$condition4["uid"] = array("in", $uid4);
							$userInfo4 = M("User") -> field("uid,type,dtttype") -> where($condition4) -> select();
							if ($userInfo4 == null) 
							{
								$userInfo4 = null;
							}
							else
							{
								foreach ($userInfo4 as $key4=>$value4)
								{
									$value4["headimg"] = "";
									$userInfo4[$key4] = $value4;
								}
							}
						}
						
						//对以上几种报名用户的情况  进行合并
						if ($userInfo1 == null && $userInfo2 == null) 
						{
							$userInfos1 = null;
						}
						elseif ($userInfo1 == null)
						{
							$userInfos1 = $userInfo2;
						}
						elseif ($userInfo2 == null)
						{
							$userInfos1 = $userInfo1;
						}
						else
						{
							$userInfos1 = array_merge($userInfo1, $userInfo2);
						}
						
						//对企业用户进行合并
						if ($userInfo3 == null && $userInfo4 == null) 
						{
							$userInfos2 = null;
						}
						elseif ($userInfo3 == null)
						{
							$userInfos2 = $userInfo4;
						}
						elseif ($userInfo4 == null)
						{
							$userInfos2 = $userInfo3;
						}
						else
						{
							$userInfos2 = array_merge($userInfo3, $userInfo4);
						}
						//对两种用户再进行合并
						if ($userInfos1 == null && $userInfos2 == null) 
						{
							$applyedPeople = array();
						}
						elseif ($userInfos1 == null)
						{
							$applyedPeople = $userInfos2;
						}
						elseif ($userInfos2 == null)
						{
							$applyedPeople = $userInfos1;
						}
						else
						{
							$applyedPeople = array_merge($userInfos1, $userInfos2);
						}
						
					} 
				}
				
				
				$AnnunciateInfo = array(
	// 					"picture"=>"http://odgzite5t.bkt.clouddn.com/".$annunciateInfo["annunciate_p"],
						"picture"=>"http://192.168.1.119/loveshow/Public/Uploads/Images/".$annunciateInfo['annunciate_p'],
						"title"=>$annunciateInfo["annunciate_r"],
						"tagname"=>array(),
						"number"=>$Number,
						"applyedPeople"=>$applyedPeople,
						"basic"=>$annunciateInfo["basic"],
						"hasapply"=>$applyedType,
						"details"=>$annunciateInfo["annunciate_w"]
				);
				
				if ($annunciateInfo == null) 
				{
					$array1['result'] = 10711;
					$array1['message'] = "获取通告详情失败";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				else
				{
					$array1['result'] = 0;
					$array1['message'] = "获取通告详情成功";
					$array1["annunciateinfo"] = $AnnunciateInfo;
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
	// 				var_dump($BasicMessage);
				}
			}
			else
			{
				$array1['result'] = 10712;
				$array1['message'] = "获取通告详情失败";
				echo json_encode($array1, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
	}